<?php

namespace app\admin\model;

use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

class WebPolicy extends BaseModel
{

    use SoftDelete;

    

    // 表名
    protected $name = 'web_policy';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'online_time_text',
        'state_text'
    ];
    
    /**
    * @param Model $row
    */
    // protected static function onAfterInsert($row){
    //     $pk = $row->getPk();
    //     $row->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
    // }

    
    public function getStateList()
    {
        return ['1' => __('资讯'), '2' => __('政策法规'), '3' => __('公告')];
    }


    public function getOnlineTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['online_time']) ? $data['online_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setOnlineTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
